<template>
  <div class="app-container">
    <el-form ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="产品项目">
        <el-input
          v-model="queryParams.productItems"
          clearable
          placeholder="请输入产品项目"
          @clear="handleQuery"
        >
          <el-button slot="append" @click="handleQuery">搜索</el-button>
        </el-input>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['erp:project:add']"
          type="primary"
          size="small"
          icon="el-icon-plus"
          @click="handleAdd"
        >
          新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['erp:project:edit']"
          type="success"
          size="small"
          icon="el-icon-edit"
          :disabled="single"
          @click="handleUpdate"
        >
          修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['erp:project:remove']"
          type="danger"
          size="small"
          icon="el-icon-delete"
          :disabled="multiple"
          @click="handleDelete"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['erp:project:export']"
          type="warning"
          size="small"
          icon="el-icon-download"
          @click="handleExport"
          >导出</el-button
        >
      </el-col>
    </el-row>
    <el-table
      v-loading="loading"
      border
      :data="projectList"
      @filter-change="handleFilterChange"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="病种"
        align="center"
        prop="firstProjiect"
        column-key="firstProjiect"
        :filters="entityOptions"
      />
      <el-table-column
        label="类别"
        align="center"
        prop="type"
        column-key="type"
        :filters="chargingProjectsTypeOptions"
      />
      <el-table-column
        label="产品项目"
        align="center"
        prop="productItems"
        width="150"
      />
      <el-table-column
        label="标签"
        align="center"
        prop="label"
        column-key="label"
        :filters="chargingProjectsTagOptions"
      ></el-table-column>
      <el-table-column label="价格" align="center" width="100" prop="price">
      </el-table-column>
      <el-table-column
        label="一级审核价格"
        align="center"
        width="120"
        prop="price1"
      >
      </el-table-column>
      <el-table-column
        label="二级审核价格"
        align="center"
        width="120"
        prop="price2"
      >
      </el-table-column>
      <el-table-column label="单位" align="center" prop="unit" />
      <el-table-column
        label="开单状态"
        align="center"
        prop="billingStatus"
        column-key="billingStatus"
        width="100"
        :filters="clinchDealBasisOptions"
      />
      <el-table-column
        label="是否上架"
        align="center"
        prop="isUp"
        column-key="isUp"
        width="100"
        :filters="shelvesStatusOptions"
      >
        <template slot-scope="scope">
          {{ shelvesStatusEnum[scope.row.isUp] }}
        </template>
      </el-table-column>
      <el-table-column
        label="前台可开单"
        align="center"
        prop="isBilling"
        min-width="120"
        column-key="isBilling"
        width="120"
        :filters="frontDeskOrderOptions"
      >
        <template slot-scope="scope">
          {{ frontDeskOrderEnum[scope.row.isBilling] }}
        </template>
      </el-table-column>
      <el-table-column
        label="是否免费"
        align="center"
        prop="isFree"
        min-width="120"
        column-key="isFree"
        :filters="whetherFreeOptions"
      >
        <template slot-scope="scope">
          {{ whetherFreeEnum[scope.row.isFree] }}
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remarks" width="180" />
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改收费项目对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="650px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="病种" prop="firstProjiect">
              <el-select
                v-model="form.firstProjiect"
                placeholder="请输选择病种"
              >
                <el-option
                  v-for="option in entityOptions"
                  :key="option.label"
                  :label="option.label"
                  :value="option.value"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="类别" prop="type">
              <el-select v-model="form.type" placeholder="请选择类别">
                <el-option
                  v-for="option in chargingProjectsTypeOptions"
                  :key="option.label"
                  :label="option.label"
                  :value="option.value"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="产品项目" prop="productItems">
              <el-input
                v-model="form.productItems"
                style="width: 195px"
                placeholder="请输入产品项目"
              />
            </el-form-item>
            <el-form-item label="标签" prop="label">
              <el-select v-model="form.label" multiple placeholder="请选择标签">
                <el-option
                  v-for="option in chargingProjectsTagOptions"
                  :key="option.label"
                  :label="option.label"
                  :value="option.value"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="价格" prop="price">
              <el-input
                v-model="form.price"
                style="width: 195px"
                placeholder="请输入价格"
              ></el-input>
            </el-form-item>
            <el-form-item label="一级审核价格" prop="price1">
              <el-input
                v-model="form.price1"
                style="width: 195px"
                placeholder="请输入一级审核价格"
              ></el-input>
            </el-form-item>
            <el-form-item label="二级审核价格" prop="price2">
              <el-input
                v-model="form.price2"
                style="width: 195px"
                placeholder="请输入二级审核价格"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单位" prop="unit">
              <el-input
                v-model="form.unit"
                style="width: 195px"
                placeholder="请输入单位"
              ></el-input>
            </el-form-item>
            <el-form-item label="开单状态" prop="billingStatus">
              <el-select
                v-model="form.billingStatus"
                placeholder="请选择开单状态"
              >
                <el-option
                  v-for="option in clinchDealBasisOptions"
                  :key="option.label"
                  :label="option.label"
                  :value="option.value"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="是否上架" prop="isUp">
              <el-select v-model="form.isUp" placeholder="请选择是否上架">
                <el-option
                  v-for="option in shelvesStatusOptions"
                  :key="option.label"
                  :label="option.label"
                  :value="option.value"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="前台可开单" prop="isBilling">
              <el-select
                v-model="form.isBilling"
                placeholder="请选择前台可开单"
              >
                <el-option
                  v-for="option in frontDeskOrderOptions"
                  :key="option.label"
                  :label="option.label"
                  :value="option.value"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="是否免费" prop="isFree">
              <el-select v-model="form.isFree" placeholder="请选择是否免费">
                <el-option
                  v-for="option in whetherFreeOptions"
                  :key="option.label"
                  :label="option.label"
                  :value="option.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注" prop="remarks">
          <el-input
            v-model="form.remarks"
            type="textarea"
            placeholder="请输入备注"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="submitForm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listProject,
  getProject,
  delProject,
  addProject,
  updateProject,
  exportProject,
} from '@/api/erp/project'
import {
  entityOptions,
  shelvesStatusOptions,
  frontDeskOrderOptions,
  whetherFreeOptions,
  clinchDealBasisOptions,
  chargingProjectsTypeOptions,
  chargingProjectsTagOptions,
  entityAllOptions,
} from '@/utils/options'
import {
  entityEnum,
  shelvesStatusEnum,
  frontDeskOrderEnum,
  whetherFreeEnum,
  clinchDealBasisEnum,
  chargingProjectsTypeEnum,
  chargingProjectsTagEnum,
} from '@/utils/enums'
import { mapState } from 'vuex'

const FROM_DATA = {
  id: undefined,
  type: undefined,
  productItems: undefined,
  label: undefined,
  price: undefined,
  price1: undefined,
  price2: undefined,
  unit: undefined,
  billingStatus: undefined,
  isUp: undefined,
  isBilling: undefined,
  isFree: undefined,
  remarks: undefined,
}

const QUERY_PARAMS = {
  pageNum: 1,
  pageSize: 10,
  firstProjiect: undefined,
  type: undefined,
  productItems: undefined,
  label: undefined,
  billingStatus: undefined,
  isUp: undefined,
  isBilling: undefined,
  isFree: undefined,
}

export default {
  name: 'ChargeProject',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 收费项目表格数据
      projectList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {},
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        firstProjiect: [
          { required: true, message: '病种不能为空', trigger: 'blur' },
        ],
        type: [{ required: true, message: '请选择类别', trigger: 'blur' }],
        productItems: [
          { required: true, message: '产品项目不能为空', trigger: 'blur' },
        ],
        label: [{ required: true, message: '请选择标签', trigger: 'blur' }],
        price: [{ required: true, message: '价格不能为空', trigger: 'blur' }],
        price1: [
          { required: true, message: '一级审核价格不能为空', trigger: 'blur' },
        ],
        price2: [
          { required: true, message: '二级审核价格不能为空', trigger: 'blur' },
        ],
        unit: [{ required: true, message: '单位不能为空', trigger: 'blur' }],
        billingStatus: [
          { required: true, message: '开单状态不能为空', trigger: 'blur' },
        ],
        isUp: [
          { required: true, message: '是否上架不能为空', trigger: 'blur' },
        ],
        isBilling: [
          {
            required: true,
            message: '前台是否可开单不能为空',
            trigger: 'blur',
          },
        ],
        isFree: [
          {
            required: true,
            message: '是否是免费项目不能为空',
            trigger: 'blur',
          },
        ],
      },
    }
  },
  computed: {
    ...mapState({
      clinicId: (state) => state.clinic.clinicId,
    }),
    entityOptions() {
      return entityAllOptions.map(({ label, value }) => ({
        text: label,
        label,
        value,
      }))
    },
    shelvesStatusOptions() {
      return shelvesStatusOptions.map(({ label, value }) => ({
        text: label,
        label,
        value,
      }))
    },
    frontDeskOrderOptions() {
      return frontDeskOrderOptions.map(({ label, value }) => ({
        text: label,
        label,
        value,
      }))
    },
    whetherFreeOptions() {
      return whetherFreeOptions.map(({ label, value }) => ({
        text: label,
        label,
        value,
      }))
    },
    clinchDealBasisOptions() {
      return clinchDealBasisOptions.map(({ label, value }) => ({
        text: label,
        label,
        value,
      }))
    },
    chargingProjectsTypeOptions() {
      return chargingProjectsTypeOptions.map(({ label, value }) => ({
        text: label,
        label,
        value,
      }))
    },
    chargingProjectsTagOptions() {
      return chargingProjectsTagOptions.map(({ label, value }) => ({
        text: label,
        label,
        value,
      }))
    },
    entityEnum() {
      return entityEnum
    },
    shelvesStatusEnum() {
      return shelvesStatusEnum
    },
    frontDeskOrderEnum() {
      return frontDeskOrderEnum
    },
    whetherFreeEnum() {
      return whetherFreeEnum
    },
    clinchDealBasisEnum() {
      return clinchDealBasisEnum
    },
    chargingProjectsTypeEnum() {
      return chargingProjectsTypeEnum
    },
    chargingProjectsTagEnum() {
      return chargingProjectsTagEnum
    },
  },
  mounted() {
    this.queryParams = { ...QUERY_PARAMS }
    this.form = { ...FROM_DATA }
    this.getList()
  },
  methods: {
    /** 查询收费项目列表 */
    async getList() {
      try {
        this.loading = true
        const { rows, total } = await listProject({
          ...this.queryParams,
          clinicId: this.clinicId,
        })
        this.projectList = rows
        this.total = total
      } finally {
        this.loading = false
      }
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = { ...FROM_DATA }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加收费项目'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getProject(id).then((response) => {
        this.form = { ...response.data, label: response.data.label.split(',') }
        this.open = true
        this.title = '修改收费项目'
      })
    },
    /** 查看按钮操作 */
    handleDetails() {},
    /** 提交按钮 */
    submitForm: function () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          const { label } = this.form
          const formData = {
            ...this.form,
            label: label.join(','),
            clinicId: this.clinicId,
          }
          if (formData.id !== undefined) {
            updateProject(formData).then((response) => {
              if (response.code === 200) {
                this.msgSuccess('修改成功')
                this.open = false
                this.getList()
              } else {
                this.msgError(response.msg)
              }
            })
          } else {
            addProject(formData).then((response) => {
              if (response.code === 200) {
                this.msgSuccess('新增成功')
                this.open = false
                this.getList()
              } else {
                this.msgError(response.msg)
              }
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm(
        '是否确认删除收费项目编号为"' + ids + '"的数据项?',
        '警告',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      )
        .then(function () {
          return delProject(ids)
        })
        .then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有收费项目数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(function () {
          return exportProject(queryParams)
        })
        .then((response) => {
          this.download(response.msg)
        })
    },
    handleFilterChange(e) {
      const [key, value] = Object.entries(e)[0]
      this.queryParams[key] = value.join(',')
      this.handleQuery()
    },
  },
}
</script>
